import 'package:flutter/material.dart';

import '../../common/common.dart';
import 'my_image.dart';

const String ossStyleStr = "x-oss-process=style/thumbnail";

class MyFace extends StatefulWidget {
  final double size;
  final BorderRadiusGeometry? borderRadius;
  final String? url;
  final String defPath;
  final VoidCallback? onTap;

  final bool isOssThumbnail;

  const MyFace({
    required this.url,
    this.size = 60.0,
    this.onTap,
    this.isOssThumbnail = true,
    Key? key,
    required this.defPath,
    this.borderRadius,
  }) : super(key: key);

  @override
  _MyFaceState createState() => _MyFaceState();
}

class _MyFaceState extends State<MyFace> {
  String url = "";

  @override
  void initState() {
    //logd("MyFace", "initState:${widget.url}");
    updateUrl();

    // TODO: implement initState
    super.initState();
  }

  @override
  void didUpdateWidget(covariant MyFace oldWidget) {
    // TODO: implement didUpdateWidget
    //logd("MyFace", "didUpdateWidget:${widget.url}");
    updateUrl();
    super.didUpdateWidget(oldWidget);
  }

  void updateUrl() {
    if (widget.url != null && widget.url!.isNotEmpty && widget.url != url) {
      url = widget.url!;
      if (widget.isOssThumbnail && ossStyleStr.isNotEmpty) {
        if (url.indexOf("?") > 0) {
          url = "$url&$ossStyleStr";
        } else {
          url = "$url?$ossStyleStr";
        }
      }
      //logd("MyFace", "updateUrl:$url");
    }
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: widget.onTap,
      child: ClipRRect(
        borderRadius: widget.borderRadius ?? BorderRadius.circular(8),
        child: url.isNotEmpty
            ? Image.network(
                url,
                width: widget.size,
                height: widget.size,
              )
            : MyImage(widget.defPath, width: widget.size, height: widget.size),
      ),
    );
  }
}
